from django.conf import settings
from django.db.models import Q
from rest_framework import serializers
from . import models
from rest_framework.exceptions import ValidationError
import os


# 只是用来创建用户的序列化器，只会使用到反序列化的功能
class CreateUserSerializer(serializers.ModelSerializer):
    checkpassword = serializers.CharField()
    class Meta:
        model = models.User
        fields = ["username",
                  "name",
                  "role",
                  "position",
                  "part_f",
                  "password",
                  'checkpassword',
                  'email'
                  ]
        extra_kwargs = {
            'password': {'write_only': True},
            'checkpassword': {'write_only': True}
        }

    def validate(self, attrs):
        checkpassword = attrs.pop('checkpassword')
        desc = attrs.get('position').name  # attrs.get('position')拿到的是position的对象
        role = attrs.get('role')
        part = attrs.get('part_f')  # 拿到的是part的对象
        position = attrs.get('position')
        is_exit = models.User.objects.filter(role=3, part_f=part)
        is_sys = models.User.objects.filter(role=1, part_f__name='老板部')
        if checkpassword != attrs.get('password'):
            raise ValidationError('两次密码不一致')
        if role == 1:
            if part.name == '老板部':
                if is_sys:
                    raise ValidationError('超级管理员的用户只能有一个，请改选管理员')
                else:
                    models.User.objects.create_user(desc=desc, **attrs)
                    return attrs
            else:
                raise ValidationError('部门和权限不一致，老板部的权限是至少管理员')
        elif role == 2:
            if part.name == '老板部':
                models.User.objects.create_user(desc=desc, **attrs)
                return attrs
            else:
                raise ValidationError('角色是管理员，部门必须选老板部')
        elif role == 3:
            if '经理' in position.name:
                user = models.User.objects.filter(role=3, part_f=part)
                if user:
                    raise ValidationError('该部门已经存在经理了！！！')
                user = models.User.objects.create_user(desc=desc, **attrs)
                # 创建经理的同时，要到部门上将经理外键添加上
                models.Part.objects.filter(id=part.id).update(manager=user)
                return attrs
            else:
                raise ValidationError('选择的职位与权限不匹配')
        elif role == 4:
            if '经理' in position.name or part.name == '老板部':
                raise ValidationError('普通员工的权限无法匹配职位')
            models.User.objects.create_user(desc=desc, **attrs)
            return attrs
        else:
            raise ValidationError('携带的权限不存在')


# 更新用户数据的序列化器
class UpdateUserView(serializers.ModelSerializer):
    class Meta:
        model = models.User
        fields = ['username', 'name', 'email', 'part_f', 'position', 'sex', 'role']

        # user = models.User.objects.get(id=self.context.get('id'))
        # print(0)
        # if user.role not in [1,3]:
        #     position = attrs.get('position')
        #     if '经理' in position.name or '系统管理员' in position.name or '老板' in position.name:
        #         print(1)
        #         raise ValidationError('该用户的权限不能担任这个职位，去权限模块先修改权限')
        # part = attrs.get('part_f')  # 拿到的是part的对象
        # is_exit = models.User.objects.filter(role=3, part_f=part)
        # if is_exit:
        #     print(2)
        #     raise ValidationError(detail='该部门已有经理，不能再有了。。')
        # try:
        #     models.User.objects.filter(id=user.id).update(**attrs)
        #     print(3)
        #     return attrs
        # except Exception as e:
        #     print(4)
        #     raise ValidationError(str(e))


class TestViewSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Part
        fields = ['name', 'manager']


# 更改登录用户的密码
class EditPasswordViewSeriazer(serializers.ModelSerializer):
    checkpassword = serializers.CharField()
    newpassword = serializers.CharField()
    username = serializers.CharField()

    class Meta:
        model = models.User
        fields = ['password', 'checkpassword', 'username', 'newpassword']
        extra_kwargs = {
            'checkpassword': {
                'write_only': True
            },
            'password': {
                'write_only': True
            }
        }

    def validate(self, attrs):
        username = attrs.get('username')
        newpassword = attrs.get('newpassword')
        password = attrs.get('password')
        checkpassword = attrs.get('checkpassword')
        if newpassword == checkpassword:
            try:
                user = models.User.objects.get(username=username)
                if user.check_password(password):
                    user.set_password(newpassword)
                    user.save()
                    return attrs
                raise ValidationError('输入的旧密码错误！！')
            except:
                raise ValidationError('修改密码失败')
        raise ValidationError('两次密码不一致')


# 职位只序列化器
class PositionReadSerializer(serializers.ModelSerializer):
    partname = serializers.CharField(source='part.name')
    label = serializers.CharField(source='name')
    value = serializers.IntegerField(source='id')

    class Meta:
        model = models.Position
        # value是id，lable是name，partname是部门
        fields = ["value", 'label', 'partname']


# 展示当前所有用户的信息的
class ShowUserSerializer(serializers.ModelSerializer):
    part_name = serializers.CharField(source='part_f.name')
    sex_name = serializers.CharField(source='get_sex_display')
    rolename = serializers.CharField(source='get_role_display')

    class Meta:
        model = models.User
        fields = ['username', 'name', 'part_name', 'email', 'get_position', 'sex', 'id', 'role', 'rolename', 'part_f',
                  'position', 'sex_name']
        extar_kwargs = {
            'id': {'read_only': True}
        }


# 获取当前登录的用户的数据
class CurrentUserSerializer(serializers.ModelSerializer):
    part = serializers.CharField(source='part_f.name')

    class Meta:
        model = models.User
        fields = ['username', 'icon', 'get_position', 'role', 'part']


# 个人中心查看个人信息
class PersonCenterViewSerializer(serializers.ModelSerializer):
    # sex = serializers.CharField(source='get_sex_display')
    position = serializers.CharField(source='position.name')
    class Meta:
        model = models.User
        fields = ['name', 'email', 'sid', 'sex', 'barthday', 'wetchat', 'position']
        extra_kwargs = {
            'position': {'read_only': True}
        }

    def validate(self, attrs):
        position = attrs.pop('position')
        try:
            models.User.objects.filter(id=self.context.get('id')).update(**attrs)
            return attrs
        except Exception as e:
            raise ValidationError(str(e))


class ManageEvectionViewSerializer(serializers.ModelSerializer):
    apllier = serializers.CharField(source='apllier.name')

    class Meta:
        model = models.Evection
        fields = ['position', 'start', 'end', 'days', 'desc', 'result', 'apllier', 'id']


# 个人中心更换头像
class PersonIconViewSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.User
        fields = ["icon"]

    def validate(self, attrs):
        id = self.context.get('id')
        request = self.context.get('request')
        file = request.FILES.get('icon')
        try:
            user = models.User.objects.get(id=id)
            user.icon = file
            user.save()
            return attrs
        except Exception as e:
            raise ValidationError(str(e))


# 部门的序列化器
class ShowPartViewSerializer(serializers.ModelSerializer):
    number = serializers.CharField()
    name = serializers.CharField()

    class Meta:
        model = models.Part
        fields = ['number', 'name', 'desc', 'id']
        extra_kwargs = {
            'id': {'read_only': True}
        }

    def validate(self, attrs):
        try:
            # 创建部门
            models.Part.objects.create(**attrs)
            # 勾出什么，返回什么
            return attrs
        except Exception as e:
            # 报错信息，可以通过 ser.error获取
            raise ValidationError(str(e))


class PartSerializer(serializers.ModelSerializer):
    value = serializers.IntegerField(source='id')
    label = serializers.CharField(source='partname')

    class Meta:
        model = models.Part
        fields = ['label', 'value']
        extra_kwargs = {
            'id': {'read_only': True}
        }


# 返回转正需要的部门和职位信息
class PartForPositiveSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Position
        fields = ['id', 'name']


class PositiveSerializer(serializers.ModelSerializer):
    value = serializers.IntegerField(source='id')
    label = serializers.CharField(source='name')
    part = serializers.CharField(source='part_f.name')
    part_id = serializers.IntegerField(source='part_f.id')
    position = serializers.CharField(source='position.name')

    class Meta:
        model = models.User
        fields = ['label', 'value', 'part', 'intime', 'position', 'part_id']


class PositiveUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.User
        fields = ['id', 'position']

    def validate(self, attrs):
        return attrs


class EmnuChildrenRightSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.EmnuChildrenRight
        field = ['right']


# EnumChildren的序列化，二级权限序列化器
class EmnuChildrenSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.EmnuChildren
        fields = ['id', 'authName', 'path', 'right', 'component']
        extra_kwargs = {
            'id': {'read_only': True}
        }


# 一级权限序列化器
class EmnuFatherSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.EmunFather
        fields = ['id', 'authName', 'icon']
        extra_kwargs = {
            'id': {'read_only': True}
        }


# 职位信息展示
class PositionViewSerializer(serializers.ModelSerializer):
    part = serializers.CharField(source='part.name')

    class Meta:
        model = models.Position
        fields = ['name', 'pnumber', 'part', 'id', 'desc']
        extra_kwargs = {
            'id': {'read_only': True}
        }

    def validate(self, attrs):
        name = attrs.get('name')
        try:
            part = attrs.pop('part')
            part = int(part['name'])  #获取部门id
            # print(attrs) 可以看拿到的数据格式
            obj= models.Position.objects.filter(name=name,part_id=part)
            if obj:
                raise ValidationError(f'在部门内已经存在:{name}')
            else:
                models.Position.objects.create(**attrs, part_id=part)
                return attrs
        except Exception as e:
            raise ValidationError(f'在部门内已经存在:{name}')


# 提供给创建职位时，下拉框选择部门
class PartForPositionViewSerializer(serializers.ModelSerializer):
    value = serializers.IntegerField(source='id')
    label = serializers.CharField(source='name')

    class Meta:
        model = models.Part
        fields = ['value', 'label', 'manager']


class YearLogdataViewSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.YearLogData
        fields = ['year']


class DeatailLogDataViewSerialize(serializers.ModelSerializer):
    class Meta:
        model = models.DetalLogdata
        fields = ['mount', 'count', 'type']


# 公司通知的序列化器

class CompanyMessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.CompanyMessage
        fields = ['title', 'created', 'content', 'id']
        extra_kwargs = {
            'id': {
                'read_only': True
            }
        }


# 部门通知的序列化器。
class PartMessageViewSerializer(serializers.ModelSerializer):
    part = serializers.CharField(source='part.name')

    class Meta:
        model = models.CompanyMessage
        fields = ['title', 'created', 'content', 'id', 'part']
        extra_kwargs = {
            'id': {
                'read_only': True
            }
        }


# 公司文件上传
class CompanyFileSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.CompanyFile
        fields = ['desc', 'id', 'created', 'name', 'filename']
        extra_kwargs = {
            'id': {
                'read_only': True
            }
        }


# 出差的反序列化器
class CreateEvectionView(serializers.ModelSerializer):
    class Meta:
        model = models.Evection
        fields = ['position', 'start', 'end', 'desc', 'checker', 'days', 'apllier']

    def create(self, validated_data):
        print(validated_data, '----------')
        return super().create(validated_data)


# 出差序列化
class ShowEvectionSerializer(serializers.ModelSerializer):
    checker = serializers.CharField(source='checker.manager.name')
    condition = serializers.CharField(source='get_condition_display')

    class Meta:
        model = models.Evection
        fields = [
            'position', 'start', 'end', 'condition',
            'checker', 'days', 'apllier', 'desc', 'result', 'id'
        ]


# 加班序列化器
class ExtraWorkViewSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.ExtraWork
        fields = ['end', 'start', 'checker', 'desc', 'image', 'hours', 'id']
        extra_kwargs = {
            'image': {'write_only': True},
            'id': {'read_only': True}
        }

    def validate(self, attrs):
        image = attrs.pop('image')
        try:
            extra = models.ExtraWork.objects.create(**attrs, applier_id=self.context.get('id'))
            extra.image = image
            extra.save()
            return attrs
        except Exception as e:
            raise ValidationError(str(e))


# 加班序列化器
class ExtraWorkViewListSerializer(serializers.ModelSerializer):
    checker = serializers.CharField(source='checker.manager.name')

    class Meta:
        model = models.ExtraWork
        fields = ['end', 'start', 'checker', 'desc', 'image', 'hours', 'id']
        extra_kwargs = {
            # 'image':{'write_only':True},
            'id': {'read_only': True},

        }


# 审核加班申请的序列化器
class ManageExtraWorkViewSerializer(serializers.ModelSerializer):
    applier = serializers.CharField(source='applier.name')

    class Meta:
        model = models.ExtraWork
        fields = ['end', 'start', 'checker', 'result', 'hours', 'id', 'applier', 'image', 'desc']


# 创建车辆
class CreateCarViewSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Car
        fields = ['carnumber', 'carseat', 'cartype', 'id']
        extrakwargs = {
            'id': {'read_only': True}
        }


# 申请用车时的需要要的车辆序列化
class CarForApplyCar(serializers.ModelSerializer):
    class Meta:
        model = models.Car
        fields = ['label', 'value']


# 创建用车申请的序列化器
class ApplyCarViewSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.ApplyCar
        fields = ['start', 'end', 'position', 'desc', 'applier', 'car', 'hours']

    def validate(self, attrs):
        start = attrs.get('start')
        end = attrs.get('end')
        attrs['hours'] = int(attrs.get('hours'))
        car = models.ApplyCar.objects.filter(
            Q(start__lte=end, end__gte=end) | Q(start__lt=start, end__gt=start) | Q(start__lte=start, end__lte=end),
            car_id=attrs.get('car'), is_delete=False)
        if not car:  # 这样说明没有时间上的冲突，这辆车可以在该时间段内被申请
            models.ApplyCar.objects.create(**attrs)
            return attrs
        raise ValidationError('该车在此时间段内已经被申请了。')


class ShowApplyCarViewSerializer(serializers.ModelSerializer):
    checker = serializers.CharField(source='checker.manager.name')
    applier = serializers.CharField(source='applier.name')
    part = serializers.CharField(source='applier.part_f.name')
    car = serializers.CharField(source='car.car')

    class Meta:
        model = models.ApplyCar
        fields = ['start', 'end', 'position', 'desc', 'car', 'hours', 'checker', 'id', 'result', 'applier', 'part']
        extra_kwargs = {
            'id': {'read_only': True}
        }


# 报销申请,创建报销申请
class ApplyReimViewSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Reim
        fields = ['desc', 'image', 'cost', 'id', 'part']
        extra_kwargs = {
            'id': {'read_only': True},
        }

    def validate(self, attrs):
        file = attrs.pop('image')
        request = self.context.get('request')
        try:
            reim = models.Reim.objects.create(**attrs, applier_id=request.auth.get('id'))
            reim.image = file
            reim.save()
        except Exception as e:
            raise ValidationError('无法生成报销')
        return attrs


# 展示报销申请需要的数据
class ShowApplyReimViewSerializer(serializers.ModelSerializer):
    applier = serializers.CharField(source='applier.name')
    part = serializers.CharField(source='part.manager.name')

    class Meta:
        model = models.Reim
        fields = ['desc', 'image', 'cost', 'id', 'applier', 'part', 'create', 'result']
        extra_kwargs = {
            'id': {'read_only': True},
            'part': {'read_only': True},
            'applier': {'read_only': True}
        }


# 人事档案
class ManageRecordViewSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Record
        fields = ['file', 'staff', 'part']

    def validate(self, attrs):
        file = attrs.pop('file')
        try:
            re = models.Record.objects.create(**attrs)
            re.file = file
            re.save()
            return attrs
        except:
            raise ValidationError('该员工已经存在，不能再创建')


# 展示职工档案
class ShowRecordSerializer(serializers.ModelSerializer):
    staff = serializers.CharField(source='staff.name')
    part = serializers.CharField(source='part.name')
    position = serializers.CharField(source='staff.position.name')
    number = serializers.CharField(source='staff.username')

    class Meta:
        model = models.Record
        fields = ['file', 'staff', 'part', 'position', 'id', 'name', 'number']


# 员工合同
class ManageConstractViewSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Contract
        fields = ['file', 'staff', 'part']

    def validate(self, attrs):
        file = attrs.pop('file')
        try:
            re = models.Contract.objects.create(**attrs)
            re.file = file
            re.save()
            return attrs
        except:
            raise ValidationError('该员工已经存在，不能再创建')


# 查看合同
class ShowConstractSerializer(serializers.ModelSerializer):
    staff = serializers.CharField(source='staff.name')
    part = serializers.CharField(source='part.name')
    position = serializers.CharField(source='staff.position.name')
    number = serializers.CharField(source='staff.username')

    class Meta:
        model = models.Record
        fields = ['file', 'staff', 'part', 'position', 'id', 'name', 'number']


# 创建会议室的序列化器
class ConferenceViewSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Conference
        fields = ['lager', 'room', 'position', 'id', 'room_lager']
        extra_kwargs = {
            'id': {'read_only': True},
            'room_lager': {'read_only': True}
        }

    def validate(self, attrs):
        try:
            attrs['lager'] = int(attrs.get('lager'))
        except Exception as e:
            raise ValidationError('会议室容量必须是数值')
        try:
            models.Conference.objects.create(**attrs)
            return attrs
        except Exception as e:
            raise ValidationError(str(e))


class ApplyConferenceViewSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.ApllyConference
        fields = ['start', 'end', 'number', 'conference']
        etxra_kwargs = {
            'part': {'read_only': True},

        }

    def validate(self, attrs):
        applier = self.context.get('applier')
        checker = models.Part.objects.get(name='行政部')
        start = attrs.get('start')
        end = attrs.get('end')
        conference = attrs.get('conference')
        co = models.ApllyConference.objects.filter(
            Q(start__lte=end, end__gte=end) | Q(start__lte=start, end__gt=start) | Q(start__lte=start, end__gte=end),
            conference=conference)
        if co:
            raise ValidationError('该会议室在该时间段被使用了')
        try:
            models.ApllyConference.objects.create(**attrs, applier_id=applier, checker=checker)
        except:
            raise ValidationError('无法申请会议室')
        return attrs


class ShowApplyConferenceserializer(serializers.ModelSerializer):
    part = serializers.CharField(source='applier.part_f.name')
    room = serializers.CharField(source='conference.room')
    checker = serializers.CharField(source='checker.manager.name')
    applier = serializers.CharField(source='applier.name')

    class Meta:
        model = models.ApllyConference
        fields = ['start', 'end', 'part', 'room', 'id', 'result', 'checker', 'position', 'applier']
        extra_kwargs = {
            'id': {'read_only': True},
            'checker': {'read_only': True}
        }


class FindPasswordSerializer(serializers.ModelSerializer):
    # unique的必须重写
    username = serializers.CharField()
    code = serializers.CharField()
    comfirepassword = serializers.CharField()

    class Meta:
        model = models.User
        fields = ['username', 'password', 'comfirepassword', 'code']

    def validate(self, attrs):
        redis = self.context.get('redis')
        username = attrs.get('username')
        redis_code = redis.get(settings.EMAILTITLE + username).decode()
        code = attrs.get('code')
        password = attrs.get('password')
        com_password = attrs.get('comfirepassword')
        if redis_code == code:
            if password == com_password:
                user = models.User.objects.get(username=username)
                user.set_password(password)
                user.save()
                return attrs
            else:
                raise ValidationError('两次密码不一致')
        else:
            raise ValidationError('验证码错误')


class AddressListSerializer(serializers.ModelSerializer):
    part = serializers.CharField(source='part_f.name')
    position = serializers.CharField(source='position.name')
    sex = serializers.CharField(source='get_sex_display')

    class Meta:
        model = models.User
        fields = ['name', 'part', 'position', 'email', 'telephone', 'icon', 'sex']


# 项目
class TaskPartserializer(serializers.ModelSerializer):
    class Meta:
        model = models.Task
        fields = ['name', 'start', 'end', 'desc', 'file', 'manage']

    def validate(self, attrs):
        file = attrs.pop('file')
        try:
            task = models.Task.objects.create(**attrs)
            task.file = file
            task.save()
        except Exception as e:
            raise ValidationError(str(e))
        return attrs


class TaskListSerializer(serializers.ModelSerializer):
    manage = serializers.CharField(source='manage.manager.name')
    part = serializers.CharField(source='manage.name')
    class Meta:
        model = models.Task
        fields = ['name', 'start', 'end', 'desc', 'manage', 'id', 'part', 'file']


# 创建该
class TaskDetailSeralizer(serializers.ModelSerializer):
    staff = serializers.CharField(source='staff.name')
    part = serializers.CharField(source='staff.part_f.name')

    class Meta:
        model = models.TaskDetail
        fields = ['content', 'create', 'staff', 'id', 'part']


class TaskToDetailSeralizer(serializers.ModelSerializer):
    task = serializers.CharField(source='task.name')
    task_id = serializers.IntegerField(source='task.id')

    class Meta:
        model = models.TaskToDetail
        fields = ['staff', 'task', 'task_id']


# 等级的序列化器
class LevelSeralizer(serializers.ModelSerializer):
    class Meta:
        model = models.Level
        fields = ['number', 'desc', 'id']
        extra_kwargs = {
            'id': {'read_only': True}
        }
#将员工任务发送给对应的员工执行
class StaffForTaskSerializer(serializers.ModelSerializer):
    part = serializers.CharField(source='part_f.name')
    position = serializers.CharField(source='position.name')
    class Meta:
        model = models.User
        fields=['name','id','part','position']
#员工个人任务的序列化器，创建
class PersonTaskSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.StaffTask
        fields=['file','desc','name','level','recever','end','apllier']
    #创建员工的个人任务
    def validate(self, attrs):
        try:
            models.StaffTask.objects.create(**attrs)
        except Exception as e:
            raise ValidationError(e)
        return attrs
#员工任务展示的序列化
class ListPersonTaskSerializer(serializers.ModelSerializer):
    staff=serializers.CharField(source='recever.name')
    part = serializers.CharField(source='recever.part_f.name')
    apllier = serializers.CharField(source='apllier.manager.name')
    level = serializers.CharField(source='level.desc')
    type=serializers.CharField(source='level.type')
    class Meta:
        model = models.StaffTask
        fields = ['file','desc','name','level','staff','end','part','apllier','id','type']
#员工任务中，详细的实现过程
class PersonTaskDetailSerializer(serializers.ModelSerializer):
    staff = serializers.CharField(source='stafftask.recever.name')
    class Meta:
        model = models.TaskStep
        fields=['content','create','staff']
#员工任务中详细过程的
class TaskStepSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.TaskStep
        fields=['content','stafftask']
    def validate(self, attrs):
        try:
            models.TaskStep.objects.create(**attrs)
        except Exception as e:
            raise ValidationError(str(e))
        return attrs
import time
#员工工资的序列化器
class OneSalaryViewSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Salary
        fields=['id','salary','absent','leave','overwork','m_time','last_salary','users']
        extra_kwargs={
            "id":{'read_only':True},
            'last_salary':{'read_only':True},
        }
    def validate(self, attrs):
        try:
            #实发工资
            last_salary = float(attrs.get('salary'))+float(attrs.get('overwork')-abs(float(attrs.get('absent'))))-abs(float(attrs.get('leave')))
            #同2022-06，只保留一份数据。
            user = attrs.get('users')
            m_time = attrs.get('m_time')
            salary = models.Salary.objects.filter(users=user,m_time=m_time)
            if salary:
                salary.update(last_salary=last_salary,**attrs)
            else:
                models.Salary.objects.create(last_salary=last_salary,**attrs)
                pass
        except Exception as e:
            raise ValidationError(e)
        return attrs
class StaffSalaryViewSerializer(serializers.ModelSerializer):
    part=serializers.CharField(source='users.part_f.name')
    position = serializers.CharField(source='users.position.name')
    username = serializers.CharField(source='users.name')
    class Meta:
        model = models.Salary
        fields=['username','part','position','overwork','absent','leave','salary','last_salary','m_time']

#员工查看签到功能的
class ShowStaffSignSerializer(serializers.ModelSerializer):
    user = serializers.CharField(source='user.name')
    part = serializers.CharField(source='user.part_f.name')
    class Meta:
        model = models.Attendance
        fields = ['user','part','startime','endtime','_is_delay','_is_leave','_is_before','_is_evection','long_time']
#每月应该签到次数
class MonthSignCountSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.MonthSignCount
        fields=['year_month','sign_days','id']

